package components.views.filters
{
	import components.views.filters.time.TimeSlider;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.containers.HBox;
	import mx.containers.VBox;
	import mx.controls.Button;
	import mx.controls.List;

	[Event(name="TIME_FILTER", source="components.views.filters.events.FilterEvent")]

	public class TimeFilter extends VBox
	{	
		// ---------------- Private Variables -------------- //
		private var sliderContainer:List;
		private var editContainer:HBox;
		// ---------------- END Private Variables -------------- //
		
		// ---------------- Initialization ----------------- //
		public function TimeFilter()
		{
			super();
			
			initComponents();
			createInitialSlider();
		}
		
		private function initComponents():void
		{
			sliderContainer = new List();
			sliderContainer.percentWidth = 100;
			sliderContainer.percentHeight = 100;
			
			this.addChild(sliderContainer);
			
			editContainer = new HBox();
			editContainer.percentWidth = 100;
			editContainer.percentHeight = 100;
			
			var remove:Button = new Button();
			remove.label = "-";
			remove.addEventListener(MouseEvent.CLICK, onRemoveSlider);
			editContainer.addChild(remove);
			
			var add:Button = new Button();
			add.label = "+";
			add.addEventListener(MouseEvent.CLICK, onAddSlider);
			editContainer.addChild(add);
			
			this.addChild(editContainer);
		}
		
		private function createInitialSlider():void
		{
			var data:ArrayCollection = new ArrayCollection();
			var slider:TimeSlider = new TimeSlider();
			sliderContainer.dataProvider = data.addItem(slider);
		}
		// ---------------- END Initialization ----------------- //
		
		// ---------------- Event Handling ------------------ //
		private function onRemoveSlider(evt:MouseEvent):void
		{
			var sliders:ArrayCollection = sliderContainer.dataProvider as ArrayCollection;
			if(sliders.length > 1)
			{
				var selected:TimeSlider = sliderContainer.selectedItem;
				sliders.removeItemAt(sliders.getItemIndex(selected));
				sliderContainer.dataProvider = sliders;
			}
			else if(sliders.length == 1)
			{
				var slider:TimeSlider = sliders.getItemAt(0) as TimeSlider;
				slider.reset();
			}
			else
				createInitialSlider();
			
		}
		
		private function onAddSlider(evt:MouseEvent):void
		{
			var slider:TimeSlider = new TimeSlider();
			sliderContainer.dataProvider.addItem(slider);
		}
		// ---------------- END Event Handling ------------------ //
	}
}